<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" href="../../component/pear/css/pear.css" />
		<link rel="stylesheet" href="../../admin/css/other/department.css"/>
	</head>
	<body class="pear-container">
		<form class="layui-form" lay-filter="form-filter">
			<div class="bottom">
				<div class="button-container">
					<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
							lay-filter="submit-filter">
						<i class="layui-icon layui-icon-ok"></i>
						提交
					</button>
					<button type="reset" class="pear-btn pear-btn-sm layui-hide">
						<i class="layui-icon layui-icon-refresh"></i>
						重置
					</button>
				</div>
			</div>
		</form>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
		<script>
			layui.use(['form', 'jquery', 'dtree', 'util'], function() {
				let form = layui.form;
				let $ = layui.jquery;
				let dtree = layui.dtree;
				let util = layui.util;

				let params = $.vtl.getQueryVariable();

				window.initFormValue = function() {
					$.ajax({
						url: "/vtl-generator/template/tree",
						dataType: 'json',
						type: 'get',
						success: function(result) {
							let data = result.data;
							let card = $("<div/>", { class: "layui-card" });
							let header = $("<div/>", { class: "layui-card-header" });
							let body = $("<div/>", { class: "layui-card-body" });
							let input = $("<input/>", { type: "checkbox"}).attr("lay-skin", "primary").attr("lay-filter", "template");
							let checkedAll = $('<input type="checkbox" name="" title="全选" lay-skin="primary" lay-filter="check-all">');
							for(let i in data) {
								let headerClone = header.clone().text(data[i].title)
									.append($('<span>&emsp;<span>'))
									.append(checkedAll.clone().attr('name', data[i].title));
								let cardClone = card.clone().append(headerClone);
								let bodyClone = body.clone();
								for(let j in data[i].children) {
									input.clone()
										.attr("name", data[i].children[j].title)
										.attr("title", data[i].children[j].title.replaceAll('{EntityName}', params.entityName))
										.attr("value", data[i].children[j].id)
										.appendTo(bodyClone);
								}
								bodyClone.appendTo(cardClone);
								$("form").children(":last-child").before(cardClone);
							}
							form.render();
						}
					})
				}

				window.initFormValue();

				form.on('submit(submit-filter)', function(data) {
					let checkedTemplateIdList = $.vtl.getCheckBoxCheckedValue('form input:checkbox[lay-filter=template]:checked');
					if(checkedTemplateIdList.length < 1) {
						layer.msg("请至少选择一个模板！", { icon: 2 });
						return false;
					}
					$.ajax({
						url: "/vtl-generator/executeBatch/" + params.tableName,
						data: JSON.stringify({ templateIdList: checkedTemplateIdList, config: params }),
						dataType: 'text',
						contentType: 'application/json',
						type: 'post',
						success: function(result) {
							layer.msg('代码已生成到文件夹：' + result, { icon: 1, time: 0, closeBtn: 1, shade: [0.5, "#393D49"] }, function() {
								parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
							});
						},
						// 下面两个不能去掉，目的是覆盖全局的代码逻辑。
						beforeSend: function (xhr) {},
						complete: function (xhr) {},
					})
					return false;
				});

				form.on('checkbox(check-all)', function(data){
					if(data.elem.checked) {
						$(data.othis).parent().siblings('[class=layui-card-body]').children('input:checkbox')
						.prop('checked', true); // 注意：这里不能使用 attr() 来设置。
						form.render("checkbox")
					} else {
						$(data.othis).parent().siblings('[class=layui-card-body]').children('input:checkbox:checked')
						.prop('checked', false);
						form.render("checkbox")
					}
				});

			util.event('lay-select-event', {
				check: function(othis) {
					othis.parent().siblings('[class=layui-card-body]').children('input:checkbox').attr('checked', true);
					form.render();
				},
				cancel: function(othis) {
					othis.parent().siblings('[class=layui-card-body]').children('input:checkbox').attr('checked', false);
					form.render();
				}
			});

			})
		</script>
	</body>
</html>
